<!--
  -    Copyright (c) 2018-2025, infomng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: infomng
  -->
<template>
  <div class="execution">
    <basic-container>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="工厂设置：">
          <el-select v-model="form.menuId" placeholder="请选择组织">
            <el-option @click.native="setChange(item)" v-for="item in organizationList" :key="item.menuId"
              :label="item.organizationName" :value="item.menuId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="组织名称：">
          {{ form.organizationName }}
        </el-form-item>
        <el-form-item label="组织别名：">
          {{ organizationAlias }}
        </el-form-item>
        <el-form-item label="设定人：">
          {{ setter }}
        </el-form-item>
        <el-form-item label="设定时间：">
          {{ setTime }}
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-if="permissions.pfoc_factorysettings_edit" @click="onSubmit">确认设定</el-button>
        </el-form-item>
      </el-form>
    </basic-container>
  </div>
</template>

<script>
import { getObj, putObj } from '@/api/pfoc/factorysettings'
import { getOrganization } from '@/api/hengdong/pfocinterface'
import { mapGetters } from 'vuex'

export default {
  name: 'factorysettings',
  data() {
    return {
      form: {
        id: 1,//默认ID
        menuId: null,//组织ID
        organizationName: null,//组织名称
      },
      organizationAlias: null,//组织别名
      setter: null,//设定人
      setTime: null,//设定时间
      organizationList: [],//组织集合
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.pfoc_factorysettings_add, false),
        delBtn: this.vaildData(this.permissions.pfoc_factorysettings_del, false),
        editBtn: this.vaildData(this.permissions.pfoc_factorysettings_edit, false)
      }
    }
  },
  created() {
    //获取组织机构
    this.getOrganizationData();
    //获取
  },
  methods: {
    //工厂选择
    setChange(row) {
      this.form.organizationName = row.organizationName;//组织名称
      this.organizationAlias = row.organizationAlias;//组织别名
    },
    //获取组织机构
    getOrganizationData() {
      getOrganization().then(response => {
        this.organizationList = response.data.data;
        this.getList();

      })
    },
    // 查询信息
    getList() {
      getObj(this.form.id).then(response => {
        this.form = response.data.data;
        for (let i = 0; i < this.organizationList.length; i++) {
          if (this.organizationList[i].menuId == this.form.menuId) {
            this.organizationName = this.organizationList[i].organizationName;//组织名称
            this.organizationAlias = this.organizationList[i].organizationAlias;//组织别名
            this.setter = this.form.setter;//设定人
            this.setTime = this.form.setTime;//设定时间
          }
        }
      })
    },
    // 设定确认
    onSubmit() {
      const t = this;
      this.$confirm('是否确认设定', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return putObj(t.form)
      }).then(data => {
        this.$message.success('设定成功')
        this.getList()
        console.log(data)
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.execution {
  .basic-container {
    border-radius: 0px;
  }

  ::v-deep .el-form {
    background: #fff;
    height: 90vh;
    box-sizing: border-box;
    padding: 15px 0;
  }

}
</style>
